<?php
/**
mysql 类
*/

class lib_db_mysql {
	
	private $link;
	private $stmt;
	private  $data=array();
	private $config;
	
	
	public function __construct($config)
	{
		$this->config=$config;
	}

	public function connect(){
		$this->link =  mysql_pconnect($this->config['db_host'].':'.$this->config['db_port'],$this->config['db_username'],$this->config['db_password']);
		if(!$this->link){
			  printf("Connect failed: %s\n", mysql_error());
    		exit();
		}else{
			if(!mysql_select_db($this->config['db_name'],$this->link)){
				printf('connect failed: %s\n',mysql_error());
				exit();
			}
		}
		if(!mysql_ping($this->link)){			
   			 printf ("Error: %s\n", mysql_error());
   			 exit();
		}
         mysql_query("SET NAMES '{$this->config['db_charset']}'",$this->link );
	}
	
	public function query($sql,$type=''){
	    mysql_query("SET NAMES '{$this->config['db_charset']}'");
		$this->stmt = mysql_query($sql);
		if(empty($type)){
		  return $this->stmt;
		}else{
		    return $this->insert_id($this->stmt);
		}
	}
	
	public function num_rows($query){
	    
		return mysql_num_rows($query);
	}
	
	public function insert_id($query){
		return mysql_insert_id();
	}
	
	public function fetchAll($sql,$type='MYSQL_ASSOC'){
		$this->data = array();
		$getfetch = $type == 'MYSQL_ASSOC' ? 'mysql_fetch_assoc' : 'mysql_fetch_row' ;
		if($this->query($sql)){
			while($row = $getfetch($this->stmt)){
				$this->data[] = $row;
			}
			return $this->data;	
		}else{
			exit('sql query fail');
		}
	}
	
	public function fetch($sql,$type='MYSQL_ASSOC'){
		$this->data = array();
		$getfetch = $type == 'MYSQL_ASSOC' ? 'mysql_fetch_assoc' : 'mysql_fetch_row' ;
		if($this->query($sql)){
			while($row = $getfetch($this->stmt)){
				$this->data = array_merge($this->data,$row);
			}
			return $this->data;	
		}else{
			exit('sql query fail');
		}
	}
	
	public function update($array,$table,$where){
		foreach ($array as $k=>$v){
			$ary[]=$k."='".$v."'";
		}
		if(is_array($ary) && sizeof($array)>0){
			$sql="update ".$table." set ".implode(',',$ary)." ".$where;
			return $this->query($sql);
		}
		return false;
	}
	
	public function insert($array,$table){
			foreach($array as $k => $v){
	        $ary[]=$k."='".$v."'";
	    }
	    $sql="insert into ".$table." set ".implode(',',$ary);
	    $query=$this->query($sql);
	    return $this->insert_id($query);
	}
	
	function table_exists($tab){
		$sql="SHOW TABLES LIKE '%".$tab."%'";
		$query=$this->query($sql);
		if($this->num_rows($query)) return true;
			else return false;
	}
	
	public function setwhereid($id,$table,$where,$con,$place=''){
			$sql="select ";
			$sql.=is_array($con)?implode(',',$con):$con;
			$sql.=" ".$where;
			$re=$this->fetch_array($sql);
			if(!empty($re)){
				$return=$place;
			}else{
				$return=is_array($con)?$re:$re[0][$con];
			}
			return $return;
	}
	
	public function free_result(){
		mysql_free_result($this->stmt);
	}
	
	public function  getValue($sql){
	    return $this->fetch($sql);
	}
	
	public function getList($sql){
	    return $this->fetchAll($sql);
	}
	
	public function __destruct(){
		//mysql_close();
	}
}
?>